/*
 * send_55mbps.c
 * Created on December 21, 2005; 8:43 PM
 * by vramadurai
 */

/************ System Header Files ***************/
#include <stdio.h>
#include <stdlib.h>

/************ Private Header Files **************/
#include "HsTypes.h"
#include "PhyDebug.h"
#include "PhyStruct.h"
#include "PmdGlobals.h"
#include "PhyConstants.h"
#include "tables.h"
#include "tx_functions.h"
#include "iqdma_3500.h"
#include "icrdefs_3500.h"
#include "thread_globals.h"

void
send_55mbps(
                struct HS_PhyTx11bStr  *HS_PhyTx11bPtrTh,
                ePHYSTATES_TH  *ePhyState,
                hs_uint8_t  *ucStateTable4Bit,
                hs_uint32_t *crctab
            )
{
	hs_uint32_t crc;
	hs_int32_t  crc_count;
	hs_uint8_t ucBit;
    hs_uint8_t ucBytePayload;
    hs_uint8_t ucInputBit;
    hs_int32_t 	flip_flag;
    hs_int32_t 	siPayloadByteCount;
    hs_uint8_t ucScramState;
    hs_uint16_t usLength;
	hs_int32_t siPayloadBitCount;
	hs_int32_t input_toggle;

	hs_uint8_t *ucInputBit_shared;
    hs_uint8_t  *psPayloadByte;
    //int tic1, tic2;

	ucInputBit_shared = HS_PhyTx11bPtrTh->ucInputBit_shared_2_55;
    ucScramState = HS_PhyTx11bPtrTh->ucScramState;
    usLength = HS_PhyTx11bPtrTh->usLength;
    psPayloadByte = (hs_uint8_t*)HS_PhyTx11bPtrTh->psPayloadByte;

	siPayloadBitCount = 0;
	siPayloadByteCount = 0;
	input_toggle = 0;

	crc = 0xFFFFFFFFU;
	crc_count = 0;

    while(1){

    	//tic1 = __sb_cfsr(MACH_CYC);
    	printf("send step 1\n ");

		// CRC
		if(siPayloadByteCount < usLength-4){
			ucBytePayload = psPayloadByte[siPayloadByteCount];
			crc ^= ucBytePayload;
			crc = crctab[crc & 0xf] ^ (crc >> 4);
			crc = crctab[crc & 0xf] ^ (crc >> 4);
		}
		else{
			if(crc_count == 0){
				crc = crc ^ 0xffffffffU;
			}
			ucBytePayload = (crc >> (8*crc_count)) & 0xFF;
			crc_count++;
		}

		siPayloadByteCount++;

		for(flip_flag=0; flip_flag<2; flip_flag++){

	        ucBit = ucBytePayload & 0xF;
	        ucBytePayload >>= 4;

			ucInputBit   = ucStateTable4Bit[ucScramState]^ucBit;
			ucScramState >>= 4;
			ucScramState |= (ucInputBit << 3);

			if(flip_flag == 1){
				ucInputBit ^= 0x3;
			}
			ucInputBit_shared[2*input_toggle+flip_flag] = ucInputBit;
		}

		printf("send step 2\n ");

		input_toggle ^= 1;

		//tic2 = __sb_cfsr(MACH_CYC);

		while(tx_thread1to2_flag1 != 0){
			SPIN(2);
        }
        tx_thread1to2_flag1 = 1;

        printf("send step 3\n ");

        siPayloadBitCount += 8;
        if(siPayloadBitCount == (usLength*8))
        {
           *ePhyState  = PHY_STATE_SWITCH_OFF_TH;
           break;
        }/* if siStateCount */
        //__putstring("cyc = %d\n", tic2-tic1);
    }
}
